<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象</title>
</head>

<body>
    <script>
        // 对象声明 1=========================================
        let student = {
            name: "张三",
            grade: "21级",
            course: ["WEB前端开发", "Java", "数据结构"],
            scores: {
                "大一": 3.9,
                "大二": 4.2
            },
            sayHi: function () {
                console.log(this.name + " Hello!!");
            },
            showCourse: function () {
                for (let cn of this.course) {
                    console.log(`课程名：${cn}`);
                }
            },
            getScore: function (gradeName) {
                let res = this.scores[gradeName] || "";
                return res;
            }
        };

        student.name = "沈工-张三";
        student.sayHi();
        student.showCourse();
        console.log("姓名：" + student.name + " 届：" + student['grade'])
        console.log("Score: " + student.getScore("大一") + " .");

        // 对象声明 2=========================================

        let student_two = {};
        student_two.name = "李四";
        student_two['name'] = "李四";
        student_two.grade = "22级";
        student_two.sayHi = function () {
            console.log(this.name + " Hello!!");
        };

        student_two.sayHi();

        // 对象声明 3=========================================
        function Student(name, grade) {
            this.name = name;
            this.grade = grade;
            this.sayHi = function () {
                console.log(this.name + " Hello!!");
            };
        }
        let student_three = new Student("王武", "20级");
        let student_four = new Student("赵六", "20级");
        student_three.sayHi();
        student_four.sayHi();

        // 对象声明 4=========================================
        function Student2(name, grade) {
            this.name = name;
            this.grade="20级";
        }
        Student2.prototype = {
            sayHi: function () {
                console.log(this.name + " Hello!!");
            }
        };
        let student_five = new Student("new王武", "20级");
        let student_six = new Student("new赵六", "20级");
        student_five.sayHi();
        student_six.sayHi();

        // 遍历对象=========================================
        for (let key in student) {
            console.log("student => " + key + " 类型：" + (typeof student[key]));
        }

    </script>
</body>

</html>